<?php
defined('_JEXEC') or die('Restricted access');
/**
 * @TODO: 
 */
?>
<script type="text/javascript">
var temp_container = {};
var user = {
    usersList : {
        requestUrl              : "user/getUsersList",
        requestType             : "get",
        htmlTableID_toListUsers : "tbl_users"
    },
    
    createUser : {
        requestUrl : "user/create",
        requestType: "post",
        formName   : "create_user"
    },
    
    editUser : {
        requestUrl : "user/edit",
        requestType: "post",
        formName   : "edit_user"
    },
    
    deleteUser : {
        requestUrl : "user/delete",
        requestType: "post",
        formName   : "deleteUsers_Form"
    }
};

user.getUsersListFromServer = function(){
    $.ajax({
        url : user.usersList.requestUrl,
        type: user.usersList.requestType,
        success: function(data, textStatus, jqXHR){
            if (tools.isNullOrUndef(data)){
                alert('Error 0x0000022: No Response from Server, please try again later...')
                return;
            }
            
            if (tools.isNullOrUndef(data.response)){
                alert('Error 0x0000032: No Response from Server, please try again later...')
                return;
            }
            if (tools.isNullOrUndef(data.data)){
                alert('0x0A001: The was an internal Server error, please try again later...');
                return;
            }
            if (data.response=='OK'){
                //here, first remove all rows (head & body) from table (if any.).
                $("#"+user.usersList.htmlTableID_toListUsers+" > thead").html("");
                $("#"+user.usersList.htmlTableID_toListUsers+" > tbody").html("");
                //    or:   $(myTable).empty(); ????
                
                var ts = "<tr>"
                ts += "<th></th>"; //for check_boxes
                for (var i in data.data.table_column_names){
                    ts +="<th>"+data.data.table_column_names[i]+"</th>";
                }
                ts += "<th></th>"; //for edit button
                ts += "</tr>";
                $("#"+user.usersList.htmlTableID_toListUsers+" thead").prepend(ts);
                
                var l = tools.objLength(data.data.table_data);
                if (l<1){
                    $("#msg").html("No Users found.<br />");
                    $("#msg").show();
                    return;
                }
                
                //list the user's data...
                var tt = data.data.table_data;
                for (i = 0; i < l; i++) {
                    ts = "<tr>";
                    ts += "<td class=\"check_box\"><input id=\"i"+tt[i].id+"\" type=\"checkbox\"></td>";
                    ts += "<td class=\"id\">"+tt[i].id+"</td>";
                    ts += "<td class=\"username\">"+tt[i].username+"</td>";
                    ts += "<td class=\"email\">"+tt[i].email+"</td>";
                    ts += "<td class=\"date_last_access\">"+tt[i].date_last_access+"</td>";
                    ts += "<td class=\"date_created\">"+tt[i].date_created+"</td>";
                    ts += "<td class=\"status\">"+tt[i].status+"</td>";
                    ts += "<td class=\"editB"+tt[i].id+"\">Edit</td>";
                    ts += "</tr>";
                    $("#"+user.usersList.htmlTableID_toListUsers+" tbody").append(ts);
                }
                $("#deleteB").show();
                return;
            } else if (data.response=='NOK') {
                if (!tools.isNullOrUndef(data.errors)){
                    alert(data.errors);
                } else {
                    alert('0x0A122: The was an internal Server error, please try again later...');
                }    
                return;
            } else {
                alert('0x0A002: The was an internal Server error, please try again later...');
                return;
            }
        },
        // callback handler that will be called on completion, which means, either on success or error
        complete: function(jqXHR, textStatus){},
        error:  function(jqXHR, textStatus, errorThrown){alert('Error: '+JSON.stringify(jqXHR, null, 4));}
    });
}
user.createNewUser = function(uName, uEmail, uPassword){

}
user.deleteUsers = function(users_ids_array){
    var lll = users_ids_array.length;
    temp_container.d = [];
    for (var kk = 0; kk<lll; kk++){
        temp_container.d.push(users_ids_array[kk].id.substr(1));
    }
    
    $.ajax({
        url:  user.deleteUser.requestUrl,
        type: user.deleteUser.requestType,
        data: {ids_to_delete: JSON.stringify(temp_container.d),
               form_name: user.deleteUser.formName
               },
        dataType: "json",
//            beforeSend: function(x) {
//                if (x && x.overrideMimeType){ x.overrideMimeType("application/j-son;charset=UTF-8"); }
//            },
        success: function(data, textStatus, jqXHR){
            if (tools.isNullOrUndef(data)){
                alert('Error 0x0000025: No Response from Server, please try again later...')
                return;
            }
            if (tools.isNullOrUndef(data.response)){
                alert('Error 0x0000045: No Response from Server, please try again later...')
                return;
            }

            if (data.response=='OK'){
                //if we receive OK from server, remove deleted records from html table
                // or just re-request UserList from server ???
                user.getUsersListFromServer();
                //alert('Ok, Deleted');
                return;
            } else if (data.response=='NOK') {
                if (!tools.isNullOrUndef(data.errors)){
                    alert(data.errors);
                } else {
                    alert('Error 0x0J002: The was an internal Server error, please try again later...');
                }
                return;
            } else {
                alert('Error 0x0J002: The was an internal Server error, please try again later...');
                return;
            }
        },
        // callback handler that will be called on completion, which means, either on success or error
        complete: function(jqXHR, textStatus){},
        error: function(jqXHR, textStatus, errorThrown){alert('Error: '+JSON.stringify(jqXHR, null, 4));}
    });
}

$(document).ready(function() {
    $.ajaxSetup ({  
        cache: false,
        timeout: 10000
    });  
    $("#msg").hide();
    $("#add_new_user_wrp").hide();
    
    $("#add_new_user_wrp_header").css({'background-color':'lightblue','padding-left':'30px'});        
    $("#add_new_user_wrp_header").mouseover(function(){$(this).css({'cursor':'pointer','background-color':'blue'});});
    $("#add_new_user_wrp_header").mouseout(function(){$(this).css({'background-color':'lightblue'});});
    $("#add_new_user_wrp_header").click(function(ev){
        if ($("#add_new_user_wrp").css('display')=='block'){
            $("#add_new_user_wrp").hide(100);
        } else {
            $("#add_new_user_wrp").show();
        }
        return false;
    });
    
    $("#groups_list").css({'width':'300px','height':'300px','border':'1px solid lightgrey','float':'left'});
    
    $("#äccess_list").css({'width':'550px','height':'300px','border':'1px solid lightgrey','margin-left':'305px'});
    
    $("#create_user").css({'background-color':'lightblue','width':'110px','margin-left':'725px'});        
    $("#create_user").mouseover(function(){$(this).css({'cursor':'pointer','background-color':'blue'});});
    $("#create_user").mouseout(function(){$(this).css({'background-color':'lightblue'});});
    $("#create_user").click(function(ev){
        
        return false;
    });
    
    $("#deleteB").hide();
    $("#deleteB").css({'background-color':'lightblue','width':'130px'});
    $("#deleteB").mouseover(function(){$(this).css({'cursor':'pointer','background-color':'blue'});});
    $("#deleteB").mouseout(function(){$(this).css({'background-color':'lightblue'});});
    $("#deleteB").click(function(ev){
        var arr = $("#"+user.usersList.htmlTableID_toListUsers+" :checked");
        var l = arr.length; var ar = [];
        for (var u=0; u<l; u++){
            if (arr[u].id.substr(0,1)=="i"){ ar.push(arr[u]); }
        }
        
        var al = ar.length;
        if (al==0){ alert("Please check one or more users to delete"); return false; }
        if (al=1){
            if (confirm("You are about to delete user with id "+ar[0].id.substr(1)+", are you sure ?")){ user.deleteUsers(ar); }
            return false;
        }
        if (al>1){
            if (confirm("You are about to delete "+al+" users, are you sure ?")){ user.deleteUsers(ar); }
            return false;
        }
        return false;
    });
    user.getUsersListFromServer();
});

</script>

<h1>Users</h1>
<hr />
<div id="msg"></div>
<div id="add_new_user_wrp_header">Add new User</div>
<div id="add_new_user_wrp">
    <div id="create_user_form">
        <input id="username_input" maxlength="16" name="username_input" placeholder="username" size="16" type="text" />
        <input id="email_input" maxlength="255" name="email_input" placeholder="email" size="32" type="text" />
        <input id="password_input1" maxlength="255" name="password_input1" placeholder="password" size="32" type="text" />
        <input id="password_input2" maxlength="255" name="password_input2" placeholder="password again" size="32" type="text" />
        <input id="checkbox_active" type="checkbox" checked="checked"/> Active
    </div>
    <div id="groups_list">Groups
       <table id="tbl_groups_list"><thead>
               <tr><td>C</td><td>Name</td><td>Description</td></tr>
           </thead>
           <tbody>
               <tr><td><input type="checkbox" /></td><td>Administrator</td><td>Description</td></tr>
               <tr><td><input type="checkbox" /></td><td>Registered User</td><td>Description</td></tr>
               <tr><td><input type="checkbox" /></td><td>Guest</td><td>Description</td></tr>
               <tr><td><input type="checkbox" /></td><td>Texnikoi</td><td>Description</td></tr>
               <tr><td><input type="checkbox" /></td><td>Pwlhtes</td><td>Description</td></tr>
               <tr><td><input type="checkbox" /></td><td>Katasthma 62Martyrwn</td><td>Description</td></tr>
           </tbody></table>
    </div>
    <div id="äccess_list">Access
       <table id="tbl_äccess_list"><thead></thead><tbody></tbody></table>
    </div><br />
    <div id="create_user">Create User</div><br />
</div>
<hr />
<div id="deleteB">Delete selected users</div>

<table id="tbl_users"><thead></thead><tbody></tbody></table>
